package barbershop.com.service.impl;

import barbershop.com.entity.Services;
import barbershop.com.dao.ServicesDao;
import barbershop.com.service.ServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ServicesServiceImpl implements ServicesService {
    @Autowired
    private ServicesDao servicesDao;

    @Override
    public Services insert(Services record) {
        Services services = new Services();
        try {
            services = this.servicesDao.save(record);
            System.out.println("插入成功！");
            return services;
        } catch (Exception e){
            System.out.println("插入失败！");
            e.printStackTrace();

        }
        return null;
    }

    @Override
    public Boolean delete(Services record) {
        try {
            this.servicesDao.delete(record);
            System.out.println("删除成功！");
            return true;
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("删除失败");
            return false;
        }
    }

    @Override
    public Services update(Services record) {
        Services serviceType = new Services();
        try {
            serviceType = this.servicesDao.save(record);
            System.out.println("修改成功！");
            return serviceType;
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("修改失败！");
        }
        return null;
    }

    @Override
    public Services find(Services record) {
        Optional<Services> serviceType;
        try {
            serviceType = this.servicesDao.findById(record.getServiceId());
            if (serviceType.isPresent())
                System.out.println("查找成功！");
            return serviceType.get();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("查找失败");
        }
        return null;
    }

    @Override
    public List<Services> findAll() {
        try {
            List<Services> servicesList = this.servicesDao.findAll();
            System.out.println("所有服务获取成功！");
            return servicesList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("所有服务获取失败！");
        }
        return null;
    }

    @Override
    public List<Services> findByType(Integer serviceTypeId) {
        try {
            List<Services> servicesList =
                    this.servicesDao.findAllByServiceType_ServiceTypeId(serviceTypeId);
            System.out.println("根据服务类型获取所有服务成功！");
            return servicesList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("根据服务类型获取所有服务失败！");
        }
        return null;
    }

    @Override
    public Page<Services> getPage(Integer page, Integer size) {
        try {
            Pageable pageable = PageRequest.of(page-1, size);
            Page<Services> servicesPage = this.servicesDao.findAll(pageable);
            System.out.println("所有服务分页完成！");
            return servicesPage;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("所有服务分页失败！");
        }
        return null;
    }

}
